package com.example;

import com.example.domain.Person;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Controller
public class Hello2Controller {
    private Logger logger =  LoggerFactory.getLogger(this.getClass());

    // 从 application.properties 中读取配置，如取不到默认值为Hello Shanhy
    @Value("${application.hello:Hello Angel}")
    private String hello;



    //thymeleaf 模板 ： pom加入依赖
//    <dependency>
//			<groupId>org.springframework.boot</groupId>
//			<artifactId>spring-boot-starter-thymeleaf</artifactId>
//		</dependency>

    //http://localhost:8080/demo/indexh.html 直接访问静态html页面，html放到static下面( /static/indexh.html)

    //http://localhost:8080/demo/leaf  静态资源放到static,页面放到templates (templates/leaf.html),浏览器直接访问页面

    @RequestMapping(value = {"/leaf"})
    public String leaf(Model model) {
        Person single = new Person("aa", 11);

        List<Person> people = new ArrayList<Person>();
        Person p1 = new Person("xx1", 11);
        Person p2 = new Person("yy", 22);
        Person p3 = new Person("zz", 33);
        people.add(p1);
        people.add(p2);
        people.add(p3);

        model.addAttribute("singlePerson", single);
        model.addAttribute("people", people);
        logger.debug("---------> logkkk, {}","debug");//?
        logger.trace("---------> logkkk, {}","trace");//?
        logger.info("---------> logkkk, {}","info");
        System.out.println("-------------hhhhhhhh-------");
        return "/leaf";
    }

    /**
     * 返回html模板.
     */
    @RequestMapping("/helloHtml")
    public String helloHtml(Map<String,Object> map){
        map.put("hello","from TemplateController.helloHtml");
        return"/helloHtml";
    }

    /**
     * 返回html模板.
     */
    @RequestMapping("/helloFtl")
    public String helloFtl(Map<String,Object> map){
        map.put("hello","from TemplateController.helloFtl");
        return"/helloFtl";
    }


    /**
     * 返回JSP模板.
     */
    @RequestMapping("/helloJsp")
    public String helloJsp(Map<String,Object> map){
        System.out.println("HelloController.helloJsp().hello="+hello);
        map.put("hello", hello);
        return "helloJsp";
    }


}
